﻿<%@ Page Language="C#" AutoEventWireup="true" CodeFile="DynamicBillMode.aspx.cs"
    Inherits="DynamicBillMode" %>

<%@ Register Assembly="Ext.Net" Namespace="Ext.Net" TagPrefix="ext" %>
<!DOCTYPE html PUBLIC "-//W3C//DTD XHTML 1.0 Transitional//EN" "http://www.w3.org/TR/xhtml1/DTD/xhtml1-transitional.dtd">
<html xmlns="http://www.w3.org/1999/xhtml">
<head runat="server">
    <title>单据模板</title>
    <script src="js/extTools.js" type="text/javascript"></script>
    <link href="css/ExtStyle.css" rel="stylesheet" type="text/css" />
    <script>
        var addRecord = function (form, grid) {

            App.Panel3.setTitle("详细资料-新增");
            App.ActState.setValue("Add");
            form.getForm().reset();
            //            alert(form.getForm().getFieldValue('BillID'))
            CompanyX.BtnAdd({
                success: function (flag) {
                    Ext.getCmp('txtBillNo').setValue(App.HI_billNO.getValue())
                }
            });
//            var text = CompanyX.BtnAdd();
//            alert(text.toString());
//            if (text != null) {

//                Ext.getCmp('txtBillNo').setValue(App.HI_billNO.getValue())
//            }
        };
        var editRecord = function (form, grid) {

            App.Panel3.setTitle("详细资料-修改");
            App.ActState.setValue("Edit");
        };
        var saveRecord = function (obj) {
            var fields = "";
            var vals = "";
            var str;
            obj.items.each(function (item, index, length) {
                if (item.getValue() != null) {
                    str = item.initialConfig.id;
                    fields += str.substring(3, str.length) + ",";
                    if (item.getXType() == "datefield") {
                        vals += Ext.util.Format.date(item.getValue(), 'Y-m-d') + ",";
                    } else
                        vals += item.getValue() + ",";
                }
            });
            if (obj.getForm().isValid()) {
                fields = fields.substring(0, fields.length - 1);
                vals = vals.substring(0, vals.length - 1);
                var mr = App.MainStore.getChangedData();
                CompanyX.BtnSave(fields, vals, JSON.stringify(mr));
                //                var modified = App.MainStore.modified;
                //                var json = [];
                //                Ext.each(modified, function (item) {
                //                    json.push(item.data);
                //                });
                //                alert(json);

                //alert(JSON.stringify(mr));
                //CompanyX.SaveDtlByGrid(JSON.stringify(mr));
            }
            //            alert(obj.getForm().getValues(true));
        }
        function insertRecord(grid, form) {
            form.getForm().reset();
            grid.store.insert(grid.store.getCount(), form.getForm().getFieldValues());
            var indx = grid.store.getCount();
            var maxid = '000' + indx.toString();
            grid.store.getAt(indx - 1).set('BillNO', App.HI_billNO.getValue()); 
            grid.store.getAt(indx - 1).set('Maxid', maxid); 
            //            var store = grid.store,
            //                row = store.indexOf(store.insert(0, form.getForm().getFieldValues())[0]);

            //            Ext.defer(function () {
            //                grid.editingPlugin.startEditByPosition({ row: row, column: 0 });
            //            }, 100);
        }
        var gridDbClick = function (record) {
            App.PageHeder.getForm().loadRecord(record);
            App.TabPanel1.setActiveTab('Panel1');
            App.HI_billNO.setValue(record.id);
            CompanyX.OpenDtl(record.id);
        }
    </script>
</head>
<body>
    <form id="form1" runat="server">
    <div>
        <ext:ResourceManager ID="ResourceManager1" runat="server" Theme="Default" />
        <ext:Viewport ID="Viewport1" runat="server">
            <LayoutConfig>
                <ext:HBoxLayoutConfig Align="Stretch" />
            </LayoutConfig>
            <Defaults>
                <ext:Parameter Name="margin" Value="0" Mode="Value" />
            </Defaults>
            <Items>
                <ext:Panel runat="server" Flex="1" Layout="FitLayout">
                    <TopBar>
                        <ext:Toolbar ID="Toolbar1" runat="server">
                            <Items>
                                <ext:Button ID="btnAdd" runat="server" Text="新增" IconCls="add24" Scale="Large" IconAlign="Top"
                                    Cls="x-btn-as-arrow" RowSpan="3">
                                    <Listeners>
                                        <Click Handler="addRecord(#{PageHeder},#{FindGridPanel})" />
                                    </Listeners>
                                </ext:Button>
                                <ext:Button ID="btnEdit" runat="server" Text="修改" IconCls="edit24" Scale="Large"
                                    IconAlign="Top" Cls="x-btn-as-arrow" RowSpan="3">
                                    <Listeners>
                                        <Click Handler="editRecord(#{PageHeder},#{FindGridPanel})" />
                                    </Listeners>
                                </ext:Button>
                                <ext:Button ID="btnSave" runat="server" Text="保存" IconCls="save24" Scale="Large"
                                    IconAlign="Top" Cls="x-btn-as-arrow" RowSpan="3">
                                    <Listeners>
                                        <Click Handler="saveRecord(#{PageHeder});">
                                        </Click>
                                    </Listeners>
                                </ext:Button>
                                <ext:Button ID="btnCancel" runat="server" Text="取消" IconCls="close24" Scale="Large"
                                    IconAlign="Top" Cls="x-btn-as-arrow" RowSpan="3">
                                    <Listeners>
                                        <Click Handler="save(#{PageHeder});">
                                        </Click>
                                    </Listeners>
                                </ext:Button>
                                <ext:ToolbarSeparator />
                                <ext:Button ID="btnDelete" runat="server" Text="删除" IconCls="del24" Scale="Large"
                                    IconAlign="Top" Cls="x-btn-as-arrow" RowSpan="3" />
                                <ext:Button ID="btnFind" runat="server" Text="查找" IconCls="find24" Scale="Large"
                                    IconAlign="Top" Cls="x-btn-as-arrow" RowSpan="3">
                                    <DirectEvents>
                                        <Click OnEvent="BtnFind" />
                                    </DirectEvents>
                                </ext:Button>
                                <ext:ToolbarSeparator />
                                <ext:Button ID="btnFirst" runat="server" Text="首条" IconCls="first24" Scale="Large" IconAlign="Top"
                                    Cls="x-btn-as-arrow" RowSpan="3" />
                                <ext:Button ID="btnPrev" runat="server" Text="上条" IconCls="previ24" Scale="Large" IconAlign="Top"
                                    Cls="x-btn-as-arrow" RowSpan="3" />
                                <ext:Button ID="btnNext" runat="server" Text="下条" IconCls="next24" Scale="Large" IconAlign="Top"
                                    Cls="x-btn-as-arrow" RowSpan="3" />
                                <ext:Button ID="btnLast" runat="server" Text="未条" IconCls="last24" Scale="Large" IconAlign="Top"
                                    Cls="x-btn-as-arrow" RowSpan="3" />
                                <ext:ToolbarSeparator />
                                <ext:SplitButton ID="btnCheck1" runat="server" Text="审核" IconCls="check124" Scale="Large"
                                    IconAlign="Top" Cls="x-btn-as-arrow" RowSpan="3">
                                    <Menu>
                                        <ext:Menu runat="server">
                                            <Items>
                                                <ext:MenuItem ID="btnCheckLog" Text="审核记录" Icon="TagBlueEdit" />
                                            </Items>
                                        </ext:Menu>
                                    </Menu>
                                </ext:SplitButton>
                                <ext:Button ID="btnColose" runat="server" Text="关闭" IconCls="close24" Scale="Large"
                                    IconAlign="Top" Cls="x-btn-as-arrow" RowSpan="3" Visible="false" />
                            </Items>
                        </ext:Toolbar>
                    </TopBar>
                    <Items>
                        <ext:TabPanel ID="TabPanel1" runat="server">
                            <Items>
                                <ext:Panel ID="Panel1" runat="server" Layout="BorderLayout" Frame="true" Title="单据信息"
                                    Icon="ApplicationViewIcons">
                                    <Items>
                                        <ext:Panel Region="North" ID="Panel3" runat="server" Split="true" Collapsible="true"
                                            Title="基本信息" Icon="PluginGo" PaddingSummary="5px 5px 0" Width="600" ButtonAlign="Center"
                                            Layout="Column">
                                            <Items>
                                                <ext:FormPanel ID="PageHeder" runat="server" Layout="Column" Height="140" Border="false"
                                                    Reference="form" ButtonAlign="Center" Padding="5">
                                                    <Items>
                                                    </Items>
                                                </ext:FormPanel>
                                            </Items>
                                        </ext:Panel>
                                        <ext:Panel runat="server" Layout="BorderLayout" Region="Center">
                                            <Items>
                                                <ext:Panel Region="East" ID="Panel2" runat="server" Split="true" Collapsible="true"
                                                    Collapsed="true" Title="详细信息" Icon="ApplicationFormEdit" PaddingSummary="5px 5px 0"
                                                    Width="550" ButtonAlign="Center" Layout="Column">
                                                    <Items>
                                                        <ext:FormPanel ID="DtlEditPanel" runat="server" Layout="Column" Border="false" Reference="form"
                                                            ButtonAlign="Center" Padding="5">
                                                            <Items>
                                                            </Items>
                                                        </ext:FormPanel>
                                                    </Items>
                                                    <Buttons>
                                                        <ext:Button ID="Button2" runat="server" Text="保存修改" Icon="Disk">
                                                            <Listeners>
                                                                <Click Handler="
                                                                    r = #{DtlEditPanel}.getForm().getRecord(); 
                                                                    if (r) {
                                                                        //#{DtlEditPanel}.getForm().updateRecord(#{GridPanel1}.getSelectionModel().getLastSelected());
                                                                    };
                                                                    saveRecord(#{EditPanel});" />
                                                            </Listeners>
                                                            <DirectEvents>
                                                                <Click />
                                                            </DirectEvents>
                                                        </ext:Button>
                                                        <ext:Button ID="Button3" runat="server" Text="取消" Icon="Cancel">
                                                            <Listeners>
                                                                <Click Handler="#{DtlEditPanel}.getForm().reset();#{Panel2}.setCollapsed(true);" />
                                                            </Listeners>
                                                        </ext:Button>
                                                    </Buttons>
                                                </ext:Panel>
                                                <ext:GridPanel ID="GridPanel1" runat="server" Region="Center">
                                                    <TopBar>
                                                        <ext:Toolbar ID="Toolbar2" runat="server">
                                                            <Items>
                                                                <ext:Button ID="btndtlAdd" runat="server" Text="新增明细" UI="Info" Icon="Add">
                                                                    <Listeners>
                                                                        <Click Handler="insertRecord(#{GridPanel1},#{DtlEditPanel});" />
                                                                    </Listeners>
                                                                </ext:Button>
                                                                <ext:Button ID="btndtlDelete" runat="server" Text="删除明细" UI="Info" Icon="Delete">
                                                                    <Listeners>
                                                                        <Click Handler="#{GridPanel1}.deleteSelected();" />
                                                                    </Listeners>
                                                                </ext:Button>
                                                                <ext:Button ID="btnRefresh" runat="server" Text="刷新" Icon="ArrowRefresh" UI="Info">
                                                                    <Listeners>
                                                                        <Click Handler="#{MainStore}.reload();" />
                                                                    </Listeners>
                                                                </ext:Button>
                                                                <ext:Button ID="Button1" runat="server" Text="Sync" Icon="Disk">
                                                                    <DirectEvents>
                                                                        <Click OnEvent="SaveClick" Before="return #{MainStore}.isDirty();">
                                                                            <ExtraParams>
                                                                                <ext:Parameter Name="data" Value="#{MainStore}.getChangedData()" Mode="Raw" Encode="true" />
                                                                            </ExtraParams>
                                                                        </Click>
                                                                    </DirectEvents>
                                                                </ext:Button>
                                                            </Items>
                                                        </ext:Toolbar>
                                                    </TopBar>
                                                    <Store>
                                                        <ext:Store ID="MainStore" runat="server" PageSize="50" OnReadData="MyData_Refresh">
                                                            <Model>
                                                                <ext:Model ID="Model3" runat="server" >
                                                                </ext:Model>
                                                            </Model>
                                                        </ext:Store>
                                                    </Store>
                                                    <ColumnModel>
                                                    </ColumnModel>
                                                    <SelectionModel>
                                                        <ext:RowSelectionModel ID="RowSelectionModel3" runat="server" />
                                                    </SelectionModel>
                                                    <Listeners>
                                                        <CellClick Handler="#{DtlEditPanel}.getForm().loadRecord(record);">
                                                        </CellClick>
                                                    </Listeners>
                                                    <Plugins>
                                                        <ext:CellEditing ID="CellEditing1" runat="server" ClicksToEdit="1" />
                                                    </Plugins>
                                                </ext:GridPanel>
                                            </Items>
                                        </ext:Panel>
                                    </Items>
                                </ext:Panel>
                                <ext:FormPanel ID="FormPanel1" runat="server" Title="单据查找" Width="1000" BodyPadding="5"
                                    Icon="Find" Layout="ColumnLayout">
                                    <FieldDefaults LabelAlign="Left" MsgTarget="Side" />
                                    <Items>
                                        <ext:GridPanel ID="FindGridPanel" runat="server" ColumnWidth="1" Height="400" DeferRowRender="true">
                                            <Store>
                                                <ext:Store ID="FindStore" runat="server">
                                                    <Model>
                                                        <ext:Model ID="Model1" runat="server">
                                                        </ext:Model>
                                                    </Model>
                                                </ext:Store>
                                            </Store>
                                            <ColumnModel ID="ColumnModel1" runat="server">
                                            </ColumnModel>
                                            <SelectionModel>
                                                <ext:RowSelectionModel ID="RowSelectionModel1" runat="server" />
                                            </SelectionModel>
                                            <Listeners>
                                                <%--<SelectionChange Handler="if (selected[0]) { this.up('form').getForm().loadRecord(selected[0]); }" />--%>
                                                <RowDblClick Handler="gridDbClick(record);">
                                                </RowDblClick>
                                            </Listeners>
                                        </ext:GridPanel>
                                    </Items>
                                </ext:FormPanel>
                            </Items>
                        </ext:TabPanel>
                    </Items>
                </ext:Panel>
                <ext:Hidden ID="ActState" runat="server" ClientIDMode="Static" Text="Browse" />
                <ext:Hidden ID="HI_billNO" runat="server" ClientIDMode="Static" Text=""/>
            </Items>
        </ext:Viewport>
    </div>
    </form>
</body>
</html>
